package com.huazi.www.drug.drugy.dao.impl;


import com.huazi.www.drug.drugy.dao.DrugDao;
import com.huazi.www.drug.drugy.entity.Drug_record;
import com.huazi.www.drug.drugy.entity.Y_drugSelect;
import com.huazi.www.drug.drugy.util.BaseDAO;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 波——最帅的男人
 * @version 1.0
 * @date 2021/5/8 15:20
 */
public class DrugDaoImpl extends BaseDAO implements DrugDao {
    /**
     * @param id       传过来的诊断号或身份证号
     * @param dataRiqi 日期
     * @param start    起始下标
     * @param end      结束下标
     * @return
     */
    @Override
    public List<Y_drugSelect> selectPage(String id, String dataRiqi, String dataRiqi2, Integer start, Integer end) {
        List<Y_drugSelect> list = new ArrayList<>();
        String sql = "select dm.userid,f.name,count(dm.medicineid),f.age,f.sex,f.phone,f.idCard,dm.time,dd.doctorname,dm.state\n" +
                "from doctor_medicine dm inner join  frontdesk_card f on(dm.userid =f.id) \n" +
                "inner join doctor_information as dd on(dd.doctorId=dm.doctorid)\n" +
                "where 1=1";

        if (!dataRiqi.equals("")) {
//            System.out.println("七天");
            //查最近七天的记录
            if (id.trim().length() == 5) {
                sql += " and dm.userid =? and  datediff(now(),dm.time)<=7 group by dm.time,dd.doctorname order by dm.time desc limit ?,?;";
                rs = executeQuery(sql, id, start, end);
            } else if (id.trim().length() == 18) {
                sql += " and f.idCard=? and  datediff(now(),dm.time)<=7 group by dm.time,dd.doctorname order by dm.time desc limit ?,?;";
                rs = executeQuery(sql, id, start, end);
            } else {
                sql += " and  datediff(now(),dm.time)<=7 group by dm.time,dd.doctorname order by dm.time desc limit ?,?;";
                rs = executeQuery(sql, start, end);
            }
        } else if (!dataRiqi2.equals("")) {
//            System.out.println("未开药");
            //查未开药的
            if (id.trim().length() == 5) {
                sql += " and dm.state=1 and dm.userid =? and  datediff(now(),dm.time)<=7 group by dm.time,dd.doctorname order by dm.time desc limit ?,?;";
                rs = executeQuery(sql, id, start, end);
            } else if (id.trim().length() == 18) {
                sql += " and dm.state=1 and f.idCard=? and  datediff(now(),dm.time)<=7 group by dm.time,dd.doctorname order by dm.time desc limit ?,?;";
                rs = executeQuery(sql, id, start, end);
            } else {
                sql += " and dm.state=1 and datediff(now(),dm.time)<=7 group by dm.time,dd.doctorname order by dm.time desc limit ?,?;";
                rs = executeQuery(sql, start, end);
            }
        } else {
//            System.out.println("全部");
            //否则为查所有时间的记录
            if (id.trim().length() == 5) {
                sql += " and dm.userid =?  group by dm.time,dd.doctorname order by dm.time desc limit ?,?";
                rs = executeQuery(sql, id, start, end);
            } else if (id.trim().length() == 18) {
                sql += " and f.idCard=?  group by dm.time,dd.doctorname order by dm.time desc limit ?,?";
                rs = executeQuery(sql, id, start, end);
            } else {
                sql += "  group by dm.time,dd.doctorname order by dm.time desc limit ?,?";
                rs = executeQuery(sql, start, end);
            }
        }
//        System.out.println(sql);
        try {
            Y_drugSelect y = null;
            while (rs.next()) {
                int anInt = rs.getInt(1);
                String string = rs.getString(2);
                int anInt1 = rs.getInt(3);
                int anInt2 = rs.getInt(4);
                String string1 = rs.getString(5);
                String string2 = rs.getString(6);
                String string3 = rs.getString(7);
                Timestamp timestamp = rs.getTimestamp(8);
                String string4 = rs.getString(9);
                int anInt3 = rs.getInt(10);
                y = new Y_drugSelect(anInt, string, anInt1, anInt2, string1, string2, string3, timestamp, string4, anInt3);
                list.add(y);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            Close(rs, ps, conn);
        }
        return list;
    }

    /**
     * 根据id查找信息
     *
     * @param id
     * @return
     */
    @Override
    public List<Drug_record> selectAll(Integer id, String riqi) {
        List<Drug_record> list = new ArrayList<>();
        String sql = "select dr.drugname,dm.sum,dm.type from doctor_medicine dm inner join drug_remedy dr on(dr.id=dm.medicineid) where dm.userid=? and dm.time=?";
        rs = executeQuery(sql, id, riqi);
        try {
            Drug_record d = null;
            if (rs != null) {
                while (rs.next()) {
                    String string = rs.getString(1);
                    int anInt = rs.getInt(2);
                    String string1 = rs.getString(3);
                    d = new Drug_record(string, anInt, string1);
                    list.add(d);
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            Close(rs, ps, conn);
        }
        return list;
    }

    /**
     * 修改状态
     * 对应的药品数量也要减少
     *
     * @param id   传过来的卡号
     * @param arr1 药品名数组
     * @param arr2 数量
     * @return
     */
    @Override
    public boolean updateState(Integer id, String riqi, String[] arr1, String[] arr2) {
        int count = 0;
        String sql = "update doctor_medicine set state=2 where userid=? and time=?";
        count = executeUpdate(sql, id, riqi);
        for (int i = 0; i < arr1.length; i++) {
            String sql1 = "update drug_remedy set count=count-? where drugname=?";
            count += executeUpdate(sql1, arr2[i], arr1[i]);
        }
//            System.out.println(count);
        if (count > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 查询返回用户状态
     *
     * @param id
     * @return
     */
    @Override
    public int selectZhuangtai(Integer id, String riqi) {
        int count = 0;
        String sql = "select state from doctor_medicine where userid=? and time=?";
        rs = executeQuery(sql, id, riqi);
        try {
            rs.next();
            count = rs.getInt(1);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            Close(rs, ps, conn);
        }
        return count;
    }

    /**
     * 查询药品数量
     *
     * @param arr1
     * @return
     */
    @Override
    public String selectArugCount(String[] arr1) {
        String str = "";
        for (int i = 0; i < arr1.length; i++) {
            String sql = "select count from drug_remedy where drugname=?";
            rs = executeQuery(sql, arr1[i]);
            try {
                rs.next();
                int count = rs.getInt(1);
                if (count < 30) {
                    str += "当前药品" + arr1[i] + "的库存不足！\n";
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
                return "数据飘走了！";
            } finally {
                Close(rs, ps, conn);
            }
        }
        if (str.equals("")) {
            return "药品充足!";
        }
        return str;
    }


}
